<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>就诊信息</title>
    <link href="css/index/index.css" rel="stylesheet" />
    <link rel="stylesheet" href="css/loginOut.css">
    <link rel="stylesheet" href="lib/css/bootstrap.css">
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/seeDoctorMessage.css">
    <script src="lib/js/jquery-1.12.4.js"></script>
    <script src="js/loginText.js"></script>
    <style>
        th,
        tr {
            text-align: center;

        }

        fieldset[disabled] {
            -ms-pointer-events: none;
            pointer-events: none;
        }
    </style>
</head>

<body>
    <!-- left 侧边栏 start -->
    <header>
        <div class="header_content">
            您好，<span class="actor"></span>！
            <div class="loginOut">
                退出
                <span class="iconfont icon-tuichu"></span>
            </div>
        </div>
    </header>
    <div class="content">
        <div id="left">
            <div class="menu">菜单栏</div>
            <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">

                <!-- todo -->


            </div>
        </div>
        <!-- left 侧边栏 end -->

        <div class="right">
            <div class="header">就诊信息</div>
            <div class="right-content">



                <div class="right-left">
                    <div class="header">我的病人</div>
                    <div class="content">
                        <div class="patientName">
                            <!-- todo -->
                        </div>
                    </div>
                </div>
                <div class="right-right">
                    <div class="header">病人信息</div>
                    <div class="content" style="padding: 0; height: 395px; overflow:auto">
                        <div>

                            <!-- Nav tabs -->
                            <ul class="nav nav-tabs" role="tablist">
                                <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab"
                                        data-toggle="tab">诊断信息</a></li>
                                <li role="presentation"><a href="#profile" aria-controls="profile" role="tab"
                                        data-toggle="tab">药品处方</a></li>
                                <li role="presentation"><a href="#messages" aria-controls="messages" role="tab"
                                        data-toggle="tab">检查项目</a></li>
                                <li role="presentation"><a href="#settings" aria-controls="settings" role="tab"
                                        data-toggle="tab">处方列表</a></li>
                            </ul>

                            <!-- Tab panes -->
                            <div class="tab-content">
                                <div role="tabpanel" class="tab-pane active jiuZhenMessage" id="home"
                                    style="text-align: left">
                                    <!-- todo -->
                                </div>
                                <div role="tabpanel" class="tab-pane pillMessage" id="profile" style="text-align: left">
                                    <!-- todo -->
                                    <div class="personMessage"></div>
                                    <div class="selectedPillMessage"></div>
                                </div>
                                <div role="tabpanel" class="tab-pane" id="messages" style="text-align: left">
                                    <div class="programPersonMessage"></div>
                                    <div class="selectedProgramMessage"></div>
                                </div>
                                <div role="tabpanel" class="tab-pane" id="settings" style="text-align: left">

                                    <div class="pill"></div>
                                    <div class="program"></div>
                                    <div><button class="btn btn-primary submitAll" style="display: none;">提交</button>
                                    </div>
                                </div>
                            </div>

                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <footer>
    </footer>
    <div class="footer_content1">权限管理系统2020</div>




</body>

<!-- 第一个页面未就诊病人就诊信息渲染 -->
<script type="text/template" id="noJiuZhenMessage">
    <fieldset>
    <table class="table table-hover">
    <thead>
        <tr>
            <th>病历号</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>身份证号</th>
            <th>科室</th>
            <th>就诊医生</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><%=data.cardNumber%></td>
            <td><%=data.name%></td>
            <td><%=data.age%></td>
            <td><%=data.number%></td>
            <td><%=data.dept%></td>
            <td><%=data.doctor%></td>
        </tr>
    </tbody>
</table>
<form class="form-inline" style="margin: 10px;">
    <div class="form-group">
        <label  >请输入发病时间(格式：2020-2-22)：</label>
        <input type="text" class="form-control time myInput" placeholder="发病时间" name="time">
    </div>
    <div class="form-group">
        <label  >请输入现病史：</label>
        <input type="text" class="form-control nowError myInput"  placeholder="现病史" name="nowError">
    </div>
    <br>
    <br>
    <div class="form-group">
        <label  >请输过往病史：</label>
        <input type="text" class="form-control pastError myInput" placeholder="过往病史" name="pastError">
    </div>
    <div class="form-group">
        <label  >请输入症状描述：</label>
        <input type="text" class="form-control error myInput" placeholder="症状描述" name="error">
    </div>
    <br>
    <br>
    <div class="form-group">
        <label  >请输入治疗计划：</label>
        <input type="text" class="form-control plan myInput" placeholder="治疗计划" name="plan">
    </div>
    <br>
    <br>
    <button type="submit" class="btn btn-primary sure" data-name="<%=data.name%>">确诊</button>
    <button type="reset" class="btn btn-primary" style="margin-left: 40px">重置</button>
</form>

</fieldset>





</script>

<!-- 第二个页面个人信息信息的渲染 -->
<script type="text/template" id="pillMessage">
    <table class="table table-hover" style="margin: 10px">
        <thead>
            <tr>
                <th>病历号</th>
                <th>姓名</th>
                <th>年龄</th>
                <th>身份证号</th>
                <th>科室</th>
                <th>就诊医生</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><%=data.cardNumber%></td>
                <td><%=data.name%></td>
                <td><%=data.age%></td>
                <td><%=data.number%></td>
                <td><%=data.dept%></td>
                <td><%=data.doctor%></td>
            </tr>
        </tbody>
    </table>  
    <button class="btn btn-primary" data-toggle="modal" data-target=".selectPills">选择药品</button>
</script>


<!-- 第三个页面个人信息信息的渲染 -->
<script type="text/template" id="programPersonMessage">
    <table class="table table-hover" style="margin: 10px">
        <thead>
            <tr>
                <th>病历号</th>
                <th>姓名</th>
                <th>年龄</th>
                <th>身份证号</th>
                <th>科室</th>
                <th>就诊医生</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><%=data.cardNumber%></td>
                <td><%=data.name%></td>
                <td><%=data.age%></td>
                <td><%=data.number%></td>
                <td><%=data.dept%></td>
                <td><%=data.doctor%></td>
            </tr>
        </tbody>
    </table>  
    <button class="btn btn-primary" data-toggle="modal" data-target=".selectPrograms">选择项目</button>
</script>

<!-- 选择药品模态框 -->
<div class="modal fade selectPills" tabindex="-1" role="dialog" aria-labelledby="gridSystemModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="gridSystemModalLabel">选择药品</h4>
            </div>
            <div class="modal-body">

                <!-- 搜素药品 -->

                <form class="form-inline my-form" style="margin-bottom: 50px" id="searchPill">
                    <div class="form-group">
                        <label for="exampleInputName2">请输入药品名字：</label>
                        <input type="text" class="form-control pillName" placeholder="输入药品名字">
                    </div>
                    <button type="submit" class="btn btn-primary">查询</button>
                </form>
                <div class="pillBox">
                    <!-- 放药品 -->
                    <!-- todo -->
                </div>
            </div>
            <div class="modal-footer">
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<!-- 选择项目模态框 -->
<div class="modal fade selectPrograms" tabindex="-1" role="dialog" aria-labelledby="gridSystemModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="gridSystemModalLabel">选择项目</h4>
            </div>
            <div class="modal-body">

                <!-- 搜素项目 -->

                <form class="form-inline" style="margin-bottom: 50px" id="searchProgram">
                    <div class="form-group">
                        <label for="exampleInputName2">请输入项目名字：</label>
                        <input type="text" class="form-control programName" placeholder="输入项目名字">
                    </div>
                    <button type="submit" class="btn btn-primary">查询</button>
                </form>
                <div class="programBox">
                    <!-- 放项目 -->
                    <!-- todo -->
                </div>
            </div>
            <div class="modal-footer">
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<!-- 所有药品渲染 -->
<script type="text/template" id="pillBox">
    <div>所有药品：</div>
    <table class="table table-hover">
    <thead>
        <tr>
            <th>操作</th>
            <th>药品名字</th>
            <th>药品编号</th>
        </tr>
    </thead>
    <tbody>
        <%for(var i = 0; i < data.length; i++) { %>
        <tr>
            <td>
                <div class="checkbox">
                    <label>
                      <input type="checkbox" name="<%=data[i].name%>" class="myCheck"> 
                    </label>
                  </div>
            </td>
            <td><%=data[i].name%></td>
            <td><%=data[i].number%></td>
        </tr>
        <%}%>
    </tbody>
</table>
<button class="btn btn-primary selectAllPills">选择</button>

</script>

<!-- 所有项目渲染 -->
<script type="text/template" id="programBox">
    <div>所有药品：</div>
    <table class="table table-hover">
    <thead>
        <tr>
            <th>操作</th>
            <th>项目名字</th>
            <th>项目编号</th>
        </tr>
    </thead>
    <tbody>
        <%for(var i = 0; i < data.length; i++) { %>
        <tr>
            <td>
                <div class="checkbox">
                    <label>
                      <input type="checkbox" name="<%=data[i].name%>" class="myCheckAllPrograms"> 
                    </label>
                  </div>
            </td>
            <td><%=data[i].name%></td>
            <td><%=data[i].number%></td>
        </tr>
        <%}%>
    </tbody>
</table>
<button class="btn btn-primary selectAllPrograms">选择</button>

</script>


<!-- 模糊查询模态框药品渲染 -->
<script type="text/template" id="moHuPillBox">

    <table class="table table-hover">
        <thead>
            <tr>
                <th>操作</th>
                <th>药品名字</th>
                <th>药品编号</th>
            </tr>
        </thead>
        <tbody>
            <%for(var i = 0; i < data.length; i++) { %>
            <tr>
                <td>
                    <div class="checkbox">
                        <label>
                          <input type="checkbox" name="<%=data[i].name%>" class="myCheckMoHu"> 
                        </label>
                      </div>
                </td>
                <td><%=data[i].name%></td>
                <td><%=data[i].number%></td>
            </tr>
            <%}%>
        </tbody>
    </table>
    <button class="btn btn-primary selectMoHuPills">选择</button>
</script>

<!-- 模糊查询模态框项目渲染 -->
<script type="text/template" id="moHuProgramBox">

    <table class="table table-hover">
        <thead>
            <tr>
                <th>操作</th>
                <th>项目名字</th>
                <th>项目编号</th>
            </tr>
        </thead>
        <tbody>
            <%for(var i = 0; i < data.length; i++) { %>
            <tr>
                <td>
                    <div class="checkbox">
                        <label>
                          <input type="checkbox" name="<%=data[i].name%>" class="myCheckMoHuProgram"> 
                        </label>
                      </div>
                </td>
                <td><%=data[i].name%></td>
                <td><%=data[i].number%></td>
            </tr>
            <%}%>
        </tbody>
    </table>
    <button class="btn btn-primary selectMoHuPrograms">选择</button>
</script>

<!-- 第二个页面新增的被选中药品内容 -->
<script type="text/template" id="pillSelectedForm">
    <table class="table table-hover">
    <thead>
        <tr>
            <th>编号</th>
            <th>药名</th>
            <th>数量</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        <%for(var i = 0; i < data.length; i++) {%>
        <tr>
            <td><%=i+1%></td>
            <td><%=data[i]%></td>
            <td>
                <input type="text" class="pillNumInput">
            </td>
            <td><button class="btn btn-primary removePill" data-target="<%=data[i]%>">删除</button></td>
        </tr>
        <%}%>
    </tbody>
</table>

<button class="btn btn-primary finalPill">保存</button>

</script>

<!-- 第三个页面新增的被选中药品内容 -->
<script type="text/template" id="programSelectedForm">
    <table class="table table-hover">
    <thead>
        <tr>
            <th>编号</th>
            <th>项目名</th>
            <th>数量</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        <%for(var i = 0; i < data.length; i++) {%>
        <tr>
            <td><%=i+1%></td>
            <td><%=data[i]%></td>
            <td>
                <input type="text" class="programNumInput">
            </td>
            <td><button class="btn btn-primary removeProgram" data-target="<%=data[i]%>">删除</button></td>
        </tr>
        <%}%>
    </tbody>
</table>

<button class="btn btn-primary finalProgram">保存</button>

</script>


<!-- 第四部分药品信息的展示 -->
<script type="text/template" id="pillFour">
    已选药品：
    <table class="table table-hover">
        <thead>
            <tr>
                <th>编号</th>
                <th>药名</th>
                <th>数量</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <%for(var i = 0; i < data.length; i++) {%>
            <tr>
                <td><%=i+1%></td>
                <td><%=data[i].name%></td>
                <td><%=data[i].num%></td>
                <td><button class="btn btn-primary removePill" data-target="<%=data[i].name%>">删除</button></td>
            </tr>
            <%}%>
        </tbody>
    </table>
</script>

<!-- 第四部分项目信息的展示 -->
<script type="text/template" id="programFour">
    已选项目：
    <table class="table table-hover">
        <thead>
            <tr>
                <th>编号</th>
                <th>项目名</th>
                <th>数量</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <%for(var i = 0; i < data.length; i++) {%>
            <tr>
                <td><%=i+1%></td>
                <td><%=data[i].name%></td>
                <td><%=data[i].num%></td>
                <td><button class="btn btn-primary removeProgram" data-target="<%=data[i].name%>">删除</button></td>
            </tr>
            <%}%>
        </tbody>
    </table>
</script>
<!-- 右边的左边病人名字渲染 -->
<script type="text/template" id="patientName">
    <%for(var i = 0; i < data.length; i++) {%>
    <div data-index="<%=i%>" class="btn btn-default patientButton" style="width:90%; margin: 5px;"><%=data[i].name%></div>
    <%}%>
</script>

<!-- 左 -->
<script type="text/template" id="myFormData">

    <%for(var i = 0; i < data.length; i++) {%>

    <div class="panel panel-default">
        <div class="panel-heading" role="tab" id="headingTwo">
            <h4 class="panel-title">
                <div class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion"
                    href="#<%=i+1%>" aria-expanded="false" aria-controls="<%=i+1%>">
                    <span><%=data[i].father%></span>
                </div>
            </h4>
        </div>
        <div id="<%=i+1%>" class="panel-collapse collapse" role="tabpanel"
            aria-labelledby="headingTwo">
            <div class="panel-body" style="color: #337AB7">
                <%for(var j = 0; j < data[i].sun.length; j++) {%>
                    <div style="margin-top: 20px"><a href="<%=data[i].sun[j].url%>"><%=data[i].sun[j].name%></a></div>
                <%}%>
            </div>
        </div>
    </div>
<% } %>

</script>







<script>
    window.onload = function () {

        // 获取当前用户信息
        var actor = JSON.parse(localStorage.getItem('userMessage')).actor
        // 获取当前医生姓名
        var doctorName = JSON.parse(localStorage.getItem('userMessage')).name
        // 左边菜单栏渲染
        getLeftCategory(function (res) {
            $('.panel-group').html(template('myFormData', {
                data: res
            }))
        }, actor)

        // 获取当前医生的病人,放在左侧
        getPatientByDoctor(function (res) {
            if (res.error) {
                window.alert(res.error)
            } else {
                // 把当前这个病人的信息存到本地
                localStorage.setItem('parentPatients', JSON.stringify(res));
                $('.patientName').html(template('patientName', {
                    data: res
                }))
            }
        }, doctorName)

        // 病人姓名按钮的点击事件
        $('.patientName').on('click', '.patientButton', function () {
            var index = this.getAttribute('data-index')
            // 把本地的这条内容取出来，显示到右边
            window.nowPatient = JSON.parse(localStorage.getItem('parentPatients'))[index]
            // 把这条数据渲染到诊断信息上
            console.log('当前病人信息')
            console.log(window.nowPatient)
            $('.jiuZhenMessage').html(template('noJiuZhenMessage', {
                data: window.nowPatient
            }))
        })


        // 确诊按钮的绑定事件
        $('.jiuZhenMessage').on('click', '.sure', function (e) {
            e.preventDefault();
            console.log(window.nowPatient)
            // 获取这个表单提交的内容
            var time = $('.time')[0].value.trim()
            var nowError = $('.nowError')[0].value.trim()
            var pastError = $('.pastError')[0].value.trim()
            var error = $('.error')[0].value.trim()
            var plan = $('.plan')[0].value.trim()
            if (!time) {
                window.alert('时间不能为空')
                return
            }
            if (!nowError) {
                window.alert('现病史不能为空')
                return
            }
            if (!pastError) {
                window.alert('过往病史不能为空')
                return
            }
            if (!error) {
                window.alert('症状描述不能为空')
                return
            }
            if (!plan) {
                window.alert('治疗计划不能为空')
                return
            }
            // 把那些输入的信息存到window.nowPatient里面
            window.nowPatient.time = time
            window.nowPatient.nowError = nowError
            window.nowPatient.pastError = pastError
            window.nowPatient.error = error
            window.nowPatient.plan = plan
            console.log(window.nowPatient)
            $('fieldset')[0].disabled = 'disabled'
            // 开始渲染第二个页面
            $('.personMessage').append(template('pillMessage', {
                data: window.nowPatient
            }))
            // 第二个页面模态框渲染
            getAllPills(function (res) {
                console.log(res)
                if (res.error) {
                    window.alert('稍后重试')
                } else {
                    $('.pillBox').html(template('pillBox', {
                        data: res
                    }))
                }
            })
            // 第三个页面模态框渲染
            getAllPrograms(function (res) {
                console.log(res)
                if (res.error) {
                    window.alert('稍后重试')
                } else {
                    $('.programBox').html(template('programBox', {
                        data: res
                    }))
                }
            })
            // 第三个页面也要放在这个渲染
            $('.programPersonMessage').append(template('programPersonMessage', {
                data: window.nowPatient
            }))

            //坑

        })

        // 在所有药品中选药事件绑定 window.arrPill存储药品名字
        $('.selectPills').on('click', '.selectAllPills', function () {
            console.log(1)
            var myCheck = $('.myCheck')
            window.arrPill = window.arrPill || []
            for (let i = 0; i < myCheck.length; i++) {
                // 
                if (myCheck[i].checked) {
                    window.arrPill.push(myCheck[i].getAttribute('name'))
                }
            }
            console.log(window.arrPill)
            // 第二个页面新增的内容
            $('.selectedPillMessage').html(template('pillSelectedForm', {
                data: window.arrPill
            }))

        })

        // 在模糊查询到的药品中选中事件的绑定 window.arrPill 存储药品名字
        $('.selectPills').on('click', '.selectMoHuPills', function () {
            var moHuPill = $('.myCheckMoHu')
            window.arrPill = window.arrPill || []
            for (let i = 0; i < moHuPill.length; i++) {
                if (moHuPill[i].checked) {
                    window.arrPill.push(moHuPill[i].getAttribute('name'))
                }
            }
            console.log(window.arrPill)
            // 第二个页面新增的内容
            $('.selectedPillMessage').html(template('pillSelectedForm', {
                data: window.arrPill
            }))
        })

        // 模糊搜索药品按钮的绑定
        $('#searchPill').submit(function (e) {
            e.preventDefault()
            // 序列化表单数据
            var pillName = $('.pillName')[0].value.trim()
            if (!pillName) {
                window.alert('药品名字不能为空！')
                return
            }
            // 发送请求查药
            $.ajax({
                url: '/pages/seeDoctorMessages/getMoHuPills',
                type: 'post',
                data: JSON.stringify({
                    pillName
                }),
                dataType: 'json',
                contentType: 'application/json;charset=UTF-8',
                success: function (res) {
                    console.log(res)
                    if (res.error) {
                        window.alert(res.error)
                    } else {
                        // 渲染模态框里面的模糊查询药品内容
                        $('.pillBox').html(template('moHuPillBox', {
                            data: res
                        }))
                    }
                }
            })
        })

        // 删除已选中药品按钮绑定事件
        $('.selectedPillMessage').on('click', '.removePill', function () {
            var pillName = this.getAttribute('data-target')
            console.log(pillName)
            for (let i = 0; i < window.arrPill.length; i++) {
                if (window.arrPill[i] == pillName) {
                    window.arrPill.splice(i, 1)
                }
            }

            console.log(window.arrPill)
            // 第二个页面新增的内容
            $('.selectedPillMessage').html(template('pillSelectedForm', {
                data: window.arrPill
            }))
        })

        // 药品保存按钮的点击事件
        $('.selectedPillMessage').on('click', '.finalPill', function () {
            var pillFinal = []
            var pillNumInput = $('.pillNumInput')
            for (let i = 0; i < pillNumInput.length; i++) {
                var pillObj = {}
                if (!pillNumInput[i].value.trim()) {
                    window.alert('数量不能为空')
                    return
                }
                pillObj.name = window.arrPill[i]
                pillObj.num = pillNumInput[i].value
                pillFinal.push(pillObj)
            }
            window.pillFinal = pillFinal
            console.log(window.pillFinal)
            // 药选好了第四个部分可以展示了
            $('.pill').html(template('pillFour', {
                data: window.pillFinal
            }))
            $('.submitAll')[0].style.display = 'block'

        })

        // 第四部分药品删除按钮绑定事件
        $('.pill').on('click', '.removePill', function () {
            var removePillName = this.getAttribute('data-target')
            console.log(removePillName)
            for (let i = 0; i < window.pillFinal.length; i++) {
                if (window.pillFinal[i].name == removePillName) {
                    window.pillFinal.splice(i, 1)
                }
            }
            // 删除药之后第四个部分重新展示
            $('.pill').html(template('pillFour', {
                data: window.pillFinal
            }))

            console.log(window.pillFinal)
        })


        // 在所有项目中选择项目事件的绑定 window.arrProgram存储项目名字
        $('.selectPrograms').on('click', '.selectAllPrograms', function () {
            console.log(1)
            var myCheck = $('.myCheckAllPrograms')
            window.arrProgram = window.arrProgram || []
            for (let i = 0; i < myCheck.length; i++) {
                // 
                if (myCheck[i].checked) {
                    window.arrProgram.push(myCheck[i].getAttribute('name'))
                }
            }
            console.log(window.arrProgram)
            // 第二个页面新增的内容
            $('.selectedProgramMessage').html(template('programSelectedForm', {
                data: window.arrProgram
            }))

        })

        // 模糊搜索项目按钮的绑定
        $('#searchProgram').submit(function (e) {
            e.preventDefault()
            // 序列化表单数据
            var programName = $('.programName')[0].value.trim()
            if (!programName) {
                window.alert('药品名字不能为空！')
                return
            }
            // 发送请求查药
            $.ajax({
                url: '/pages/seeDoctorMessages/getMoHuPrograms',
                type: 'post',
                data: JSON.stringify({
                    programName
                }),
                dataType: 'json',
                contentType: 'application/json;charset=UTF-8',
                success: function (res) {
                    console.log(res)
                    if (res.error) {
                        window.alert(res.error)
                    } else {
                        // 渲染模态框里面的模糊查询药品内容
                        $('.programBox').html(template('moHuProgramBox', {
                            data: res
                        }))
                    }
                }
            })
        })

        // 在模糊查询到的项目中选中事件的绑定 window.arrProgram 存储项目名字
        $('.selectPrograms').on('click', '.selectMoHuPrograms', function () {
            var moHuProgram = $('.myCheckMoHuProgram')
            window.arrProgram = window.arrProgram || []
            for (let i = 0; i < moHuProgram.length; i++) {
                if (moHuProgram[i].checked) {
                    window.arrProgram.push(moHuProgram[i].getAttribute('name'))
                }
            }
            console.log(window.arrProgram)
            // 第二个页面新增的内容
            $('.selectedProgramMessage').html(template('programSelectedForm', {
                data: window.arrProgram
            }))
        })

        // 删除已选中项目按钮绑定事件
        $('.selectedProgramMessage').on('click', '.removeProgram', function () {
            var programName = this.getAttribute('data-target')
            console.log(programName)
            for (let i = 0; i < window.arrProgram.length; i++) {
                if (window.arrProgram[i] == programName) {
                    window.arrProgram.splice(i, 1)
                }
            }

            console.log(window.arrProgram)
            // 第三个页面新增的内容
            $('.selectedProgramMessage').html(template('programSelectedForm', {
                data: window.arrProgram
            }))
        })


        // 项目保存按钮的点击事件
        $('.selectedProgramMessage').on('click', '.finalProgram', function () {
            var programFinal = []
            var programNumInput = $('.programNumInput')
            for (let i = 0; i < programNumInput.length; i++) {
                var programObj = {}
                if (!programNumInput[i].value.trim()) {
                    window.alert('数量不能为空')
                    return
                }
                programObj.name = window.arrProgram[i]
                programObj.num = programNumInput[i].value
                programFinal.push(programObj)
            }
            window.programFinal = programFinal
            console.log(window.programFinal)
            // 药选好了第四个部分可以展示了
            $('.program').html(template('programFour', {
                data: window.programFinal
            }))
            $('.submitAll')[0].style.display = 'block'

        })

        // 第四部分项目删除按钮绑定事件
        $('.program').on('click', '.removeProgram', function () {
            var removeProgramName = this.getAttribute('data-target')
            console.log(removeProgramName)
            for (let i = 0; i < window.programFinal.length; i++) {
                if (window.programFinal[i].name == removeProgramName) {
                    window.programFinal.splice(i, 1)
                }
            }
            // 删除药之后第四个部分重新展示
            $('.program').html(template('programFour', {
                data: window.programFinal
            }))

            console.log(window.programFinal)
        })


        // 最后一步：数据提交到后台。。。。。。。。。。。。
        // 提交的是：window.programFinal 和 window.pillFinal,病人的姓名 window.nowPatient.name,医生的姓名
        // var doctorName = JSON.parse(localStorage.getItem('userMessage')).name
        //科室： window.nowPatient.dept
        $('.submitAll').on('click', function () {
            console.log(1)
            $.ajax({
                url: '/pages/seeDoctorMessage/submitAll',
                type: 'post',
                data: JSON.stringify({
                    patient: window.nowPatient,
                    pills: window.pillFinal,
                    programs: window.programFinal
                }),
                dataType: 'json',
                contentType: 'application/json;charset=UTF-8',
                success: function (res) {
                    if (res.error) {
                        window.alert(res.error)
                    } else {
                    window.alert('处方添加成功！')
                    self.location = 'seeDoctorMessage.html'
                    }
                }
            })
        })
    }


    // 根据获取左边菜单栏函数封装
    function getLeftCategory(callback, actor) {
        $.ajax({
            url: '/pages/category/getAllCategoryAndSunCategoryByActor',
            type: 'post',
            data: JSON.stringify({
                actor
            }),
            dataType: 'json',
            contentType: 'application/json;charset=UTF-8',
            success: function (res) {
                callback && callback(res)
            }

        })
    }

    // 根据医生名字查询医生的病人，包含已就诊和未就诊
    function getPatientByDoctor(callback, doctorName) {
        $.ajax({
            url: '/pages/getPatientByDoctor',
            type: 'post',
            data: JSON.stringify({
                doctorName
            }),
            dataType: 'json',
            contentType: 'application/json;charset=UTF-8',
            success: function (res) {
                callback && callback(res)
            }

        })
    }

    // 获取所有药品函数封装
    function getAllPills(callback) {
        $.ajax({
            url: '/pages/seeDoctorMessages/getAllPills',
            type: 'post',
            data: '',
            dataType: 'json',
            contentType: 'application/json;charset=UTF-8',
            success: function (res) {
                callback && callback(res)
            }

        })
    }

    // 获取所有项目函数封装
    function getAllPrograms(callback) {
        $.ajax({
            url: '/pages/seeDoctorMessages/getAllPrograms',
            type: 'post',
            data: '',
            dataType: 'json',
            contentType: 'application/json;charset=UTF-8',
            success: function (res) {
                callback && callback(res)
            }

        })
    }

</script>

<!-- <script src="js/common.js"></script> -->
<script src="lib/js/bootstrap.js"></script>
<script src="lib/js/bootstrap-paginator.js"></script>
<script src="lib/js/template-native.js"></script>

<script src="lib/js/mock.js"></script>
<script src="mock/getCategoryByActor.js"></script>
<script src="mock/seeDoctorMessage.js"></script>
</html>